<template>
<div>
  <home-header></home-header>
  <home-swiper :list='swiperList'></home-swiper>
  <home-icons :list='iconList'></home-icons>
  <home-recommed :list='recommendList'></home-recommed>
  <home-weekend :list='weekendList'></home-weekend>
</div>
</template>

<script>
import HomeHeader from './components/header.vue'
import HomeSwiper from './components/swiper.vue'
import HomeIcons from './components/icons.vue'
import HomeRecommed from './components/recommend.vue'
import HomeWeekend from './components/weekend.vue'
import axios from 'axios'
import {mapState} from 'vuex'
export default {
  name: 'Home',
  components:{
    HomeHeader,
    HomeSwiper,
    HomeIcons,
    HomeRecommed,
    HomeWeekend
  },
  data(){
    return{
      swiperList:[],
      iconList:[],
      recommendList:[],
      weekendList:[],
      lastcity:''
    }
  },
  computed:{
    ...mapState(['city'])
  },
  methods:{
    getHomeInfo(){
      axios.get('static/mock/index.json?city='+this.city).then(this.getHomeInfoSucc)
    },
    getHomeInfoSucc(res){
      res=res.data;
      if(res.ret&&res.data){
        const data=res.data;
        this.swiperList=data.swiperList;
        this.iconList=data.iconList;
        this. recommendList=data. recommendList;
        this.weekendList=data.weekendList
      }
    }
  },
  mounted(){
    this.lastcity=this.city
    this.getHomeInfo()
  },
  activated(){//使用keep-alive自带的生命周期钩子,当页面被从新显示时执行
    if(this.lastcity!==this.city){
      this.lastcity=this.city;
      this.getHomeInfo()
    }
  }
}
</script>

<style>

</style>
